package com.ckm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckm.entity.CourseType;
import com.ckm.exception.BusinessException;
import com.ckm.mapper.CourseTypeMapper;
import com.ckm.service.CourseTypeService;
import com.ckm.util.FindObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CourseTypeServiceImpl implements CourseTypeService {
    
    @Autowired
    private CourseTypeMapper courseTypeMapper;
    
    @Override
    public void addCourseType(List<CourseType> courseTypeList) {
        if (courseTypeList == null || courseTypeList.isEmpty()) {
            throw new BusinessException(250,"添加数据集为空");
        }
        for (CourseType courseType : courseTypeList) {
            if (!findCourseType(courseType).isEmpty()) {
                throw new BusinessException(250,"数据重复:"+courseType);
            }
            courseTypeMapper.insert(courseType);
        }
        log.info("添加数据：{}", courseTypeList);
    }

    @Override
    public void deleteCourseType(Integer id) {
        if (id==null){
            throw new BusinessException(250,"删除参数为空");
        }
        if (courseTypeMapper.selectById(id)==null){
            throw new BusinessException(250,"无此课程类型");
        }
        courseTypeMapper.deleteById(id);
    }

    @Override
    public void updateCourseType(CourseType courseType) {
        if (courseType.getId()==null){
            throw new BusinessException(250,"更新信息缺少id");
        }
        CourseType select = courseTypeMapper.selectById(courseType.getId());
        if (select==null){
            throw new BusinessException(250,"无此课程类型");
        }
        if (select.equals(courseType)){
            throw new BusinessException(250,"信息无更改");
        }
        courseTypeMapper.updateById(courseType);
    }

    @Override
    public PageInfo<CourseType> getCourseTypePage(CourseType courseType, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(findCourseType(courseType));
    }

    @Override
    public List<CourseType> findCourseType(CourseType courseType) {
        QueryWrapper<CourseType> wrapper = new QueryWrapper<>();
        FindObjectUtil.fullQueryWrapper(courseType,wrapper);
        return courseTypeMapper.selectList(wrapper);
    }
}
